package com.hushao.lims.weblims.service.impl;

import com.hushao.lims.weblims.domain.ConsumableInfo;
import com.hushao.lims.weblims.domain.ConsumableRepertory;
import com.hushao.lims.weblims.domain.ConsumableStorage;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.repository.ConsumableStorageRepository;
import com.hushao.lims.weblims.service.ConsumableRepertoryService;
import com.hushao.lims.weblims.service.ConsumableService;
import com.hushao.lims.weblims.service.ConsumableStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author 930588706
 * @date 18/1/22
 * 耗材入库
 */
@Service
public class ConsumableStorageServiceImpl implements ConsumableStorageService {
    @Autowired
    private ConsumableService consumableService;
    @Autowired
    private ConsumableStorageRepository consumableStorageRepository;
    @Autowired
    private ConsumableRepertoryService consumableRepertoryService;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public ConsumableStorage saveConsumableStorage(ConsumableStorage consumableStorage) throws Exception{
        ConsumableInfo consumableInfo=null;
            //耗材入库（待审核）
        String consumableName=consumableStorage.getConsumableInfo().getConsumableName();
        String consumableSpecifications=consumableStorage.getConsumableInfo().getConsumableSpecifications();
        BigDecimal consumablePrice=consumableStorage.getConsumableInfo().getConsumablePrice();
        consumableInfo=consumableService.findByConsumableNameAndConsumableSpecificationsAndConsumablePrice(consumableName,consumableSpecifications,consumablePrice);
        if(consumableInfo==null){
            //添加耗材信息
            ConsumableInfo c=new ConsumableInfo(consumableName,consumableSpecifications,consumablePrice,null);
            consumableInfo=consumableService.save(c);
        }
        consumableStorage.setConsumableInfo(consumableInfo);
        return consumableStorageRepository.save(consumableStorage);
    }

    @Override
    public Page<ConsumableStorage> listConsumableStorageByTeacherAndConsumableName(String consumberName, TeacherInfo teacherInfo, Pageable pageable)throws Exception {
        consumberName="%"+consumberName+"%";
        return consumableStorageRepository.findByTeacherInfoAndConsumableInfo_ConsumableNameLike(teacherInfo,consumberName,pageable);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void remove(Long consumableStorageId) throws Exception {
        ConsumableStorage consumableStorage =consumableStorageRepository.getOne(consumableStorageId);
        if(consumableStorage==null){
            throw  new Exception("此耗材入库记录不存在！");
        }else if(!"0".equals(consumableStorage.getConsumableStorageState())){
            throw new Exception("此耗材入库已被审核！");
        }
        consumableStorageRepository.deleteById(consumableStorageId);
    }

    @Override
    public Page<ConsumableStorage> listConsumableStorageByConsumableNameLikeAndStateLike(String consumableName, String state, Pageable pageable) {
        consumableName="%"+consumableName+"%";
        state="%"+state+"%";
        return consumableStorageRepository.findByConsumableInfo_ConsumableNameLikeAndConsumableStorageStateLike(consumableName,state,pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ConsumableStorage audit(Long consumableStorageId, String state, TeacherInfo teacher) throws Exception {
        ConsumableStorage consumableStorage=consumableStorageRepository.getOne(consumableStorageId);
        if(consumableStorage==null){
            throw new Exception("此耗材入库不存在");
        }
        if (!"0".equals(consumableStorage.getConsumableStorageState())){
            throw new Exception("此耗材入库信息已被审核！");
        }
        consumableStorage.setConsumableStorageState(state);
        consumableStorage.setConsumableStorageAudit(teacher);
        return consumableStorageRepository.save(consumableStorage);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ConsumableStorage achieve(Long consumableStorageId) throws Exception {
        ConsumableStorage consumableStorage=consumableStorageRepository.getOne(consumableStorageId);
        if(consumableStorage==null){
            throw new Exception("此耗材入库不存在");
        }
        if ("0".equals(consumableStorage.getConsumableStorageState())){
            throw new Exception("此耗材入库信息未被审核！");
        }
        if (!"1".equals(consumableStorage.getConsumableStorageState())){
            throw new Exception("此耗材入库信息未被通过！");
        }
        Integer count=consumableStorage.getConsumableCount();
        ConsumableInfo consumableInfo=consumableStorage.getConsumableInfo();
        ConsumableRepertory consumableRepertory=consumableRepertoryService.findByConsumable(consumableInfo);
        if(consumableRepertory==null){
            consumableRepertory=new ConsumableRepertory(consumableInfo,count,null);
        }else {
            consumableRepertory.setConsumableCount(consumableRepertory.getConsumableCount()+count);
        }
        consumableRepertoryService.saveOrUpdate(consumableRepertory);
        consumableStorage.setConsumableStorageState("3");
        return consumableStorageRepository.save(consumableStorage);
    }
}
